package com.youkeyi.ddy.cloud.application.applet.system.todo.controller;

import com.youkeyi.ddy.cloud.common.api.application.applet.system.todo.AppletTodoCloudService;
import com.youkeyi.ddy.cloud.common.api.application.applet.system.todo.dto.AppletTodoListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.applet.system.todo.dto.AppletTodoListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.applet.system.todo.param.AppletTodoCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.applet.system.todo.param.AppletTodoDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.applet.system.todo.param.AppletTodoListCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.applet.system.todo.param.AppletTodoUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.todo.PublicTodoCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.todo.dto.PublicTodoListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.todo.param.PublicTodoCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.todo.param.PublicTodoDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.todo.param.PublicTodoQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.todo.param.PublicTodoUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * AppletTodoController
 *
 * @Author liuxiawang
 * @Date 2024/4/1 4:40 PM
 * @Location shenzhen.china
 */
@Service
public class AppletTodoController implements AppletTodoCloudService {

    @Autowired
    private PublicTodoCloudService publicTodoCloudService;

    @Override
    public AppletTodoListCloudDto todoList(AppletTodoListCloudParam param) {
        return this.list(param, 1);
    }

    @Override
    public AppletTodoListCloudDto doneList(AppletTodoListCloudParam param) {
        return this.list(param, 2);
    }

    private AppletTodoListCloudDto list(AppletTodoListCloudParam param, Integer status) {
        AppletTodoListCloudDto dto = new AppletTodoListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        PublicTodoQueryCloudParam todoQueryCloudParam = new PublicTodoQueryCloudParam();
        // todoQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
        todoQueryCloudParam.setUserKeys(Collections.singletonList(YoukeyiSystemContext.getUserPrimaryKey()));
        todoQueryCloudParam.setContentFuzzy(param.getContentFuzzy());
        todoQueryCloudParam.setStatuses(Collections.singletonList(status));
        todoQueryCloudParam.setTypes(param.getTypes());
        todoQueryCloudParam.setPageSize(param.getPageSize());
        todoQueryCloudParam.setCurrentPage(param.getCurrentPage());
        PublicTodoListCloudDto todoDtos = publicTodoCloudService.query(todoQueryCloudParam);
        if (null != todoDtos && CollectionUtils.isNotEmpty(todoDtos.getRows())) {
            List<AppletTodoListItemCloudDto> items = new ArrayList<>();
            todoDtos.getRows().forEach(todo -> {
                AppletTodoListItemCloudDto item = new AppletTodoListItemCloudDto();
                item.setTodoPrimaryKey(todo.getTodoPrimaryKey());
                item.setType(todo.getType());
                item.setTypeName(todo.getTypeName());
                item.setUserKey(todo.getUserKey());
                item.setCustomerKey(todo.getCustomerKey());
                item.setContent(todo.getContent());
                item.setStatus(todo.getStatus());
                item.setFinishTime(todo.getFinishTime());
                item.setRemark(todo.getRemark());
                item.setIsDeleted(todo.getIsDeleted());
                item.setIsDeletedName(todo.getIsDeletedName());
                item.setCreateUserKey(todo.getCreateUserKey());
                item.setCreateTime(todo.getCreateTime());
                item.setUpdateUserKey(todo.getUpdateUserKey());
                item.setUpdateTime(todo.getUpdateTime());
                item.setCreateUserName(todo.getCreateUserName());
                item.setUpdateUserName(todo.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(todoDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent finish(AppletTodoUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getTodoPrimaryKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        PublicTodoUpdateCloudParam todoUpdateCloudParam = new PublicTodoUpdateCloudParam();
        todoUpdateCloudParam.setTodoPrimaryKey(param.getTodoPrimaryKey());
        todoUpdateCloudParam.setStatus(2);
        todoUpdateCloudParam.setFinishTime(System.currentTimeMillis());
        todoUpdateCloudParam.setRemark(param.getRemark());
        todoUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        todoUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return publicTodoCloudService.update(todoUpdateCloudParam);
    }

    @Override
    public ResponseContent create(AppletTodoCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getTodoRemindType()) || StringUtils.isBlank(param.getTodoContent())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        if ("in-mail".equals(param.getTodoRemindType())) {
            PublicTodoCreateCloudParam todoCreateCloudParam = new PublicTodoCreateCloudParam();
            todoCreateCloudParam.setCorpKey(YoukeyiSystemContext.getCorpPrimaryKey());
            todoCreateCloudParam.setUserKey(YoukeyiSystemContext.getUserPrimaryKey());
            todoCreateCloudParam.setCustomerKey(param.getCustomerKey());
            todoCreateCloudParam.setContent(param.getTodoContent());
            todoCreateCloudParam.setStatus(1);
            todoCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
            todoCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
            return publicTodoCloudService.create(todoCreateCloudParam);
        }
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return rc;
    }

    @Override
    public ResponseContent delete(AppletTodoDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || CollectionUtils.isEmpty(param.getTodoPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        PublicTodoDeleteCloudParam todoDeleteCloudParam = new PublicTodoDeleteCloudParam();
        todoDeleteCloudParam.setTodoPrimaryKeys(param.getTodoPrimaryKeys());
        todoDeleteCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        todoDeleteCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return publicTodoCloudService.delete(todoDeleteCloudParam);
    }
}
